﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net;
using Newtonsoft.Json;
using Microsoft.SqlServer.Server;
using System.Net.Http.Headers;

namespace WebAPIHelper
{
    public class ResponseMessageHelper
    {
        public static HttpResponseMessage SUCCESS
        {
            get
            {
                HttpResponseMessage response = new HttpResponseMessage()
                {
                    Content = new StringContent(JsonConvert.SerializeObject(SUCCESS_MESSAGE)),
                    StatusCode = HttpStatusCode.OK
                };
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                return response;
            }
        }

        public static HttpResponseMessage Fail
        {
            get
            {
                var response = new HttpResponseMessage()
                {
                    Content = new StringContent(JsonConvert.SerializeObject(ERROR_MESSAGE)),
                    StatusCode = HttpStatusCode.BadRequest
                };
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                return response;
            }
        }

        public static HttpResponseMessage CreateErrorMessage(Exception ex, string errorMsg = null)
        {
            ResponseContent content = new ResponseContent()
            {
                code = "500",
                message = errorMsg ?? ex.Message,
                description = ex.StackTrace
            };
            var response = new HttpResponseMessage()
            {
                Content = new StringContent(JsonConvert.SerializeObject(content)),
                StatusCode = HttpStatusCode.BadRequest
            };
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            return response;
        }

        private static ResponseContent SUCCESS_MESSAGE = new ResponseContent()
        {
            code = "200",
            message = "SUCCESS"
        };

        private static ResponseContent ERROR_MESSAGE = new ResponseContent()
        {
            code = "400",
            message = "ERROR OCCURS"
        };
    }

    public class ResponseContent
    {
        public string code { get; set; }
        public string message { get; set; }
        public string description { get; set; }
    }
}
